import { BaseGame, IGame, IGameAction, IGameActionResult, IStorage } from "book-script";
import { GameManager } from "book-script";

@GameManager()
export class DemoGame extends BaseGame {

    constructor(storage: IStorage) {
        super(storage);
        debugger;
    }
    /**
     * 处理具体的游戏操作
     */
    protected async processAction(game: IGame, action: IGameAction): Promise<IGameActionResult> {
        // 根据操作类型执行不同的逻辑
        switch (action.type) {
            case 'move':
                return this.handleMoveAction(game, action);
            case 'talk':
                return this.handleTalkAction(game, action);
            case 'use_item':
                return this.handleUseItemAction(game, action);
            default:
                return {
                    success: false,
                    error: `未知的操作类型: ${action.type}`
                };
        }
    }

    /**
     * 处理移动操作
     */
    private handleMoveAction(game: IGame, action: IGameAction): IGameActionResult {
        const { location } = action.data;

        if (!location) {
            return {
                success: false,
                error: '缺少位置信息'
            };
        }

        const updatedGame = { ...game };
        updatedGame.data.customData.currentLocation = location;
        updatedGame.updatedAt = Date.now();

        return {
            success: true,
            updatedGame,
            data: { newLocation: location }
        };
    }

    /**
     * 处理对话操作
     */
    private handleTalkAction(game: IGame, action: IGameAction): IGameActionResult {
        const { target, message } = action.data;

        if (!target || !message) {
            return {
                success: false,
                error: '缺少对话目标或消息内容'
            };
        }

        const updatedGame = { ...game };
        if (!updatedGame.data.customData.conversations) {
            updatedGame.data.customData.conversations = [];
        }

        updatedGame.data.customData.conversations.push({
            playerId: action.playerId,
            target,
            message,
            timestamp: Date.now()
        });

        updatedGame.updatedAt = Date.now();

        return {
            success: true,
            updatedGame,
            data: { conversation: { target, message } }
        };
    }

    /**
     * 处理使用物品操作
     */
    private handleUseItemAction(game: IGame, action: IGameAction): IGameActionResult {
        const { itemId, target } = action.data;

        if (!itemId) {
            return {
                success: false,
                error: '缺少物品ID'
            };
        }

        const updatedGame = { ...game };
        if (!updatedGame.data.customData.itemUsage) {
            updatedGame.data.customData.itemUsage = [];
        }

        updatedGame.data.customData.itemUsage.push({
            playerId: action.playerId,
            itemId,
            target,
            timestamp: Date.now()
        });

        updatedGame.updatedAt = Date.now();

        return {
            success: true,
            updatedGame,
            data: { itemUsed: { itemId, target } }
        };
    }
}